<template>
  <div class="wrap">
    <van-nav-bar class="nav" title="火箭娘">
      <template #left>
        <van-icon
          name="arrow-left"
          class="left-icon"
          @click="$router.push('/my')"
        ></van-icon>
      </template>
    </van-nav-bar>
    <div class="main" ref="chatContent">
      <ul class="talk_list">
        <div v-for="(item, index) in mesList" :key="index">
          <li
            class="left_word"
            v-if="item.type === 'other'"
            @click="play(item.con)"
          >
            <img src="@/assets/long.png" />
            <span
              >{{ item.con }}
              <van-icon class="yi-icon" name="play-circle-o"></van-icon>
            </span>
          </li>
          <li class="right_word" v-else>
            <img src="@/assets/隐忍.png" /> <span>{{ item.con }}</span>
          </li>
        </div>
      </ul>
      <div class="drag_bar" style="display: none;">
        <div
          class="drager ui-draggable ui-draggable-handle"
          style="display: none; height: 412.628px;"
        ></div>
      </div>
    </div>
    <div class="footer">
      <van-field
        class="input_txt"
        v-model="inputVal"
        rows="1"
        autosize
        type="textarea"
        placeholder="说点什么吧..."
      />
      <input type="button" value="发 送" class="input_sub" @click="sendMess" />
    </div>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  data () {
    return {
      inputVal: '',
      mesList: []
    }
  },
  mounted () {
    this.scrollToBottom()
  },
  updated () {
    this.scrollToBottom()
  },
  methods: {
    play (mes) {
      const synth = window.speechSynthesis
      const msg = new SpeechSynthesisUtterance()
      msg.text = mes
      synth.speak(msg)
    },
    scrollToBottom () {
      this.$nextTick(() => {
        this.$refs.chatContent.scrollTop = this.$refs.chatContent.scrollHeight
      })
    },
    sendMess () {
      this.mesList.push({ type: 'my', con: this.inputVal })

      axios({
        url: 'http://www.tuling123.com/openapi/api',
        method: 'post',
        data: {
          userid: 778899,
          key: '932f61cbc52b46a4bccc6e166a210c5b',
          info: this.inputVal
        }
      }).then(data => {
        console.log(data.data.text)
        this.mesList.push({ type: 'other', con: data.data.text })
      })
      this.inputVal = ''
    }
  }
}
</script>

<style lang="less">
.wrap {
  height: 100%;
  position: fixed;
  width: 100%;
  left: 50%;
  transform: translateX(-50%);
  top: 0px;
  bottom: 20px;
  border: 1px solid #ebebeb;
  background-color: #fff;
  box-shadow: 0 0 30px rgba(0, 0, 0, 0.1);
  overflow: hidden;

  .nav {
    background: #d0584d;
    font-family: 'HanziPenTC-W3';
    .van-nav-bar__title {
      font-size: 24px;
      color: white;
    }
    .left-icon {
      color: #fff;
      font-size: 20px;
    }
  }
}
.header {
  height: 46px;
  background: #d0584d;
  overflow: hidden;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.header h3 {
  color: #faf3fc;
  font-weight: normal;
  float: left;
  letter-spacing: 2px;
  margin-left: 25px;
  font-size: 16px;
  text-shadow: 0px 0px 5px #d0584d;
}
.header img {
  float: right;
  margin-right: 16px;
  border-radius: 50%;
  width: 30px;
  height: 30px;
  box-shadow: 0 0 3px #f7f2fe;
}
.main {
  position: absolute;
  left: 0;
  right: 0;
  top: 46px;
  bottom: 50px;
  background-color: #f4f3f3;
  box-sizing: border-box;
  padding: 10px 0;
  overflow: auto;
}
.talk_list {
  position: absolute;
  width: 100%;
  left: 0px;
  top: 0px;
}
.talk_list li {
  overflow: hidden;
  margin: 20px 0px 6px;
}
.talk_list .left_word img {
  float: left;
  margin-left: 20px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
}
.talk_list .left_word {
  span {
    float: left;
    background-color: #fe9697;
    padding: 10px 15px;
    max-width: 200px;
    border-radius: 12px;
    font-size: 16px;
    color: #fff;
    margin-left: 13px;
    position: relative;
    line-height: 24px;
    word-wrap: break-word;
    word-break: break-all;

    .yi-icon {
      font-size: 20px;
      position: relative;
      top: 4px;
  }
  }

}
.talk_list .left_word span:before {
  content: '';
  position: absolute;
  left: -8px;
  top: 3px;
  width: 13px;
  height: 12px;
  background: url('~@/assets/corner01.png') no-repeat;
}
.talk_list .right_word img {
  float: right;
  margin-right: 20px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
}
.talk_list .right_word span {
  float: right;
  background-color: #fff;
  padding: 10px 15px;
  max-width: 200px;
  border-radius: 12px;
  font-size: 16px;
  color: #000;
  margin-right: 13px;
  position: relative;
  line-height: 24px;
  word-wrap: break-word;
  word-break: break-all;
}
.talk_list .right_word span:before {
  content: '';
  position: absolute;
  right: -8px;
  top: 3px;
  width: 13px;
  height: 12px;
  background: url('~@/assets/corner02.png') no-repeat;
}
.drag_bar {
  position: absolute;
  right: 0px;
  top: 0px;
  background-color: #fff;
  height: 100%;
  width: 6px;
  box-sizing: border-box;
  border-bottom: 1px solid #f4f3f3;
}
.drager {
  position: absolute;
  left: 0px;
  top: 0px;
  background-color: #cdcdcd;
  height: 100px;
  width: 6px;
  border-radius: 3px;
  cursor: pointer;
}

.footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  min-height: 50px;
  max-height: 80px;
  left: 0px;
  bottom: 0px;
  background-color: #fff;
  position: absolute;
  overflow: hidden;
  padding: 2px 0px;
}

.footer img {
  margin-left: 8px;
  width: 30px;
  height: 30px;
  border-radius: 50%;
}

.input_txt {
  width: 270px;
  min-height: 30px;
  max-height: 60px;
  border: 0px;
  background-color: #f4f3f3;
  margin: 5px 20px;
  border-radius: 4px;
  padding: 0px;
  outline: none;
  text-indent: 15px;
  font-size: 14px;
  line-height: 20px;
  .van-field__control {
    padding: 4px;
  }
}
.input_sub {
  width: 70px;
  height: 30px;
  border: 0px;
  font-size: 14px;
  background-color: #fe9697;
  margin-right: 16px;
  border-radius: 4px;
  padding: 0px;
  outline: none;
  color: #fff;
  cursor: pointer;
}
</style>
